package com.topisv.tms.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.ExpressStyle;
import com.topisv.tms.entity.LogisticsCompany;


/**
 * 快递单风格DAo
 * @author Administrator
 *
 */
public interface ExpressStyleDao extends PagingAndSortingRepository<ExpressStyle, Long>{
	
	List<ExpressStyle> findByCustomerCodeAndAreaGroupCode(String customerCode,String areaGroupCode);
	
	@Query("select expressStyle from ExpressStyle expressStyle where customerCode = ? and areaGroupCode = ? and expressStyle.logisticsCompany.id=?")
	List<ExpressStyle> findByCustomerCodeAndAreaGroupCodeAndLogisticsCompanyId(String customerCode,String areaGroupCode,Long logistics_company_id);
	
	@Query("select e from ExpressStyle e where e.logisticsCompany.companyCode=?1 and e.areaGroupCode=?2 and e.cainiaoFlg=?3 and (?4 is null or e.customerCode = ?4)")
	List<ExpressStyle> findByLogisticsCompanyAndAreaGroupCode(String companyCode,String areaGroupCode,String cainiaoFlg,String customerCode);
	
	@Query("select expressStyle from ExpressStyle expressStyle where expressStyle.logisticsCompany.id=?1")
	List<ExpressStyle> findStyleNames(Long logistics_company_id);
	
	@Query("select e from ExpressStyle e where  e.customerCode=?")
	List<ExpressStyle> findByStyleDufault(String customerCode);
	
	@Query("select e from ExpressStyle e where e.styleDefault=1")
	List<ExpressStyle> findByStyleDufault();
	
	@Query("select e from ExpressStyle e where e.logisticsCompany.id=?1 and e.customerCode=?2 and e.styleDefault=1 ")
	ExpressStyle findStyleDefault(Long logistics_company_id,String customerCode);
	
	@Query("select e from ExpressStyle e where e.logisticsCompany.companyCode=?1 and e.customerCode=?2 and e.areaGroupCode=?3 ")
	List<ExpressStyle> findStyle(String companyCode,String customerCode,String areaGroupCode);
	
	@Query("select e from ExpressStyle e where e.logisticsCompany.companyCode=?1 and (e.customerCode=?2 or e.cainiaoFlg = 1) and e.areaGroupCode=?3 and e.type=?4")
	List<ExpressStyle> findStyleNew(String companyCode,String customerCode,String areaGroupCode,String type);
	
	@Query("select e from ExpressStyle e where (e.customerCode=?1 or e.cainiaoFlg = 1) and e.areaGroupCode=?2 and e.type=?3")
	List<ExpressStyle> findStyleNew(String customerCode,String areaGroupCode,String type);
	
	@Modifying
	@Query("update ExpressStyle e set e.styleDefault=0 where e.logisticsCompany.id=?1 and e.customerCode=?2")
	void updateStyleDufault(Long logistics_company_id,String customerCode);
	
	@Modifying
	@Query("update ExpressStyle expressStyle set expressStyle.styleDefault=0 where expressStyle.id=?1")
	void deleteStyleDufault(Long expressStyleId);
	
	@Query("select expressStyle from ExpressStyle expressStyle where expressStyle.logisticsCompany.id=?1 and expressStyle.styleDefault=1")
	ExpressStyle findLogistics(Long intDefault);
	
	@Modifying
	@Query("delete from ExpressStyle expressStyle where expressStyle.logisticsCompany.id=?1")
	void deleteLogisticsId(Long logisticsId);
}
